﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Simulation.Framework
{
    public class SingleBehavior<T> where T : ManagerBehavior, new()
    {
        private static T _instance;
        public static T getInstance()
        {
            if (_instance == null)
            {
                _instance = Object.FindObjectOfType<T>();
            }

            if (_instance == null)
            {
                GameObject go = new GameObject(typeof(T).Name);
                _instance = go.AddComponent<T>();
                Object.DontDestroyOnLoad(go);
            }
            return _instance;
        }

        public static void Destroy()
        {
            if (_instance != null)
            {
                _instance.Clear();
                Object.Destroy(_instance.gameObject);
                _instance = null;
            }
        }
    }
}